package com.online.web.signature;

import java.security.MessageDigest;

public class MD5Utils {
    private static final String CHARSET = "UTF-8";
    private static final String SIGN_TYPE = "MD5";

    /**
     * 生成 MD5
     *
     * @param data 待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance(SIGN_TYPE);
            byte[] array = md.digest(data.getBytes(CHARSET));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) |
                        0x100).substring(1, 3));
            }
            return sb.toString().toUpperCase();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String signStr = "action=equity&appId=1&timestamp=1629352706&key=2514d8a41c7bf5583ea57004 ";
        String s = MD5(signStr);
        System.out.println(s);
    }
}
